import {Component, OnInit} from '@angular/core';
import {AddDraFreeLotteryComponent} from '../add-dra-free-lottery.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {DraFreeLottery} from '../../dra-free-lottery.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {isNullOrUndefined} from 'util';
import {GooBrandCondition} from '../../../../goods-management/goo-brand/goo-brand.type';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';
import { filter, map } from 'rxjs/operators';

@Component({
    selector: 'app-main-add-dra-free-lottery',
    templateUrl: './main-add-dra-free-lottery.component.html',
    styleUrls: ['./main-add-dra-free-lottery.component.css']
})
export class MainAddDraFreeLotteryComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain = new DraFreeLottery();

    extend = {
        domain: {
            gooBaseGoods: {
                items: [],
                // icon: fa.faSearchPlus,
                nickname: '基础属性',
                change: (event) => {
                    const domain = event.domain;
                    const uupper = domain.uuid;
                    domain.uuid = '';
                    domain.serNum = '';
                    Object.assign(this.domain, domain);
                    this.domain.upper = uupper;
                    console.log(domain);
                }
            },
            serGoodsDrawBrand: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '品牌类型',
                change: (event) => {
                    console.log(event);
                }
            },
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
            serGoodsDrawStartDate: {
                icon: fa.faCalendar,
                nickname: '活动开始时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsDrawStartDate;
                },
                focus: (event) => {
                    this.domain.serGoodsDrawStartDate = event.target.value;
                },
                blur: (event) => {
                }
            },
            serGoodsDrawEndDate: {
                icon: fa.faCalendar,
                nickname: '活动结束时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsDrawEndDate;
                },
                focus: (event) => {
                    this.domain.serGoodsDrawEndDate = event.target.value;
                },
                blur: (event) => {
                }
            },
            serGoodsDrawOpenDate: {
                icon: fa.faCalendar,
                nickname: '活动开奖时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsDrawOpenDate;
                },
                focus: (event) => {
                    this.domain.serGoodsDrawOpenDate = event.target.value;
                },
                blur: (event) => {
                }
            },
            fileGoodsDrawP2: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawP2.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawP2.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP2 = this.extend.domain.fileGoodsDrawP2.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawP2.oss.fileUrl = '';
                            this.domain.serGoodsDrawP2 = this.extend.domain.fileGoodsDrawP2.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileGoodsDrawP3: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawP3.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawP3.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP3 = this.extend.domain.fileGoodsDrawP3.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawP3.oss.fileUrl = '';
                            this.domain.serGoodsDrawP3 = this.extend.domain.fileGoodsDrawP3.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileGoodsDrawP4: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawP4.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawP4.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP4 = this.extend.domain.fileGoodsDrawP4.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawP4.oss.fileUrl = '';
                            this.domain.serGoodsDrawP4 = this.extend.domain.fileGoodsDrawP4.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileGoodsDrawView: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileGoodsDrawView.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileGoodsDrawView.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawView = this.extend.domain.fileGoodsDrawView.oss.fileUrl;
                        } else {
                            this.extend.domain.fileGoodsDrawView.oss.fileUrl = '';
                            this.domain.serGoodsDrawView = this.extend.domain.fileGoodsDrawView.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            files: {
                fileUrls: [],
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.files.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.files.fileUrls.push(JSON.parse(response).detail.fileUrl);
                            this.extend.domain.files.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsDrawP1 = this.extend.domain.files.fileUrls.toString();
                        } else {
                            this.extend.domain.files.oss.fileUrl = '';
                            this.domain.serGoodsDrawP1 = this.extend.domain.files.fileUrls.toString();
                            Zero3Alert.req('fail');
                        }
                    });
                },
                del: {
                    click: (event, fileUrl) => {
                        const urlIndex = this.extend.domain.files.fileUrls.findIndex(item => item === fileUrl);
                        this.extend.domain.files.fileUrls.splice(urlIndex, 1);
                        this.domain.serGoodsDrawP1 = this.extend.domain.files.fileUrls.toString();
                    }
                }
            },
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.draFreeLottery.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.draFreeLottery.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addDraFreeLottery: AddDraFreeLotteryComponent) {
        this.addDraFreeLottery.main = this;
        // ng select
        this.domain.serGoodsDrawBrand = null;
        // ng select
        this.domain.serGoodsDrawPeople = null;
    }

    ngOnInit() {
        this.extend.domain.default();
        Services.baseGoods.list()
        .pipe(filter(item => !isNullOrUndefined(item) && item.code === 'success'),
        map(item => {
            const result = [];
            item.page.content.forEach(base => {
                result.push({id: base.uuid, name: base.serGoodsName, domain: base});
            });
            return result;
        }))
        .subscribe(result => {
            this.extend.domain.gooBaseGoods.items = result;
        });
        //
        Services.gooBrand.list(new GooBrandCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.serGoodsDrawBrand.items = [];
                item.page.content.forEach(gooBrand => {
                    this.extend.domain.serGoodsDrawBrand.items.push({id: gooBrand.uuid, name: gooBrand.serBarndName});
                });
            }
        });
    }

}
